package com.amikomgamedev.ramashinta.entity;

import java.io.IOException;
import java.io.InputStream;

import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.bitmap.BitmapTexture;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TextureRegionFactory;
import org.andengine.ui.activity.BaseGameActivity;
import org.andengine.util.adt.io.in.IInputStreamOpener;

import com.amikomgamedev.ramashinta.Utils;
import com.amikomgamedev.ramashinta.graphic.Graphic_Detach;
import com.amikomgamedev.ramashinta.graphic.Graphic_Path;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class Bar 
{
	BaseGameActivity mainActivity;
	public Sprite spr[] = new Sprite[3];
	private int onLeft=1;
	
	public Bar(BaseGameActivity mainActivity, float x, float y, Graphic_Detach g, int side)
	{
		this.mainActivity = mainActivity;
		Load(Graphic_Path.BAR_1, x, y, 0);
		Load(Graphic_Path.BAR_2, x, y, 1);
		Load(Graphic_Path.BAR_3, x, y, 2);
		
		g.spr_Gameplay_bg.attachChild(spr[0]);
		g.spr_Gameplay_bg.attachChild(spr[1]);
		g.spr_Gameplay_bg.attachChild(spr[2]);
		if(side==onLeft){
			spr[2].setFlippedHorizontal(true);
		}
	}
	
	public void Load(String path, float x, float y, int index)
	{	
		final String _path = path;
		try 
		{
			Bitmap bitmap = BitmapFactory.decodeStream(mainActivity.getAssets().open(path));
			
			ITexture gameSpriteITexture = new BitmapTexture(mainActivity.getTextureManager(), new IInputStreamOpener() 
				{
					public InputStream open() throws IOException 
					{
						return mainActivity.getAssets().open(_path);
					}
				});
			gameSpriteITexture.load();
			ITextureRegion gameSpriteTextureRegion = TextureRegionFactory
					.extractFromTexture(gameSpriteITexture);
			spr[index] = new Sprite(0, 0, Utils.getRatioW(bitmap.getWidth()), Utils.getRatioH(bitmap.getHeight()), gameSpriteTextureRegion.deepCopy(), mainActivity.getVertexBufferObjectManager());
			spr[index].setPosition(Utils.getRatioW(x), Utils.getRatioH(y));
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
	}
}
